### This file creates taking sides (congruence) and responsiveness plots
rm(list=ls(all=TRUE))
library(ggplot2)
library(gridExtra)
require(fBasics)
library(arm)
library(foreign)
library(stringr)
library(car)
library(abind)
library(readstata13)
library(reshape2)
library(plyr)
library(data.table)
library(reshape)



setwd("C:/Users/adamz/Dropbox/RSF Ineqaulity Project/Replication File/Formatted Data")
path1 <- "C:/Users/adamz/Dropbox/RSF Ineqaulity Project/Replication File"


ggtheme_personalized <- theme(panel.background = element_blank(),
                              panel.grid = element_blank(),
                              axis.ticks = element_blank(),
                              axis.title.x = element_text(color = "black" , size = 12, face="bold"),
                              axis.title.y = element_text(color = "black" , size = 12, face="bold"),
                              axis.text.y = element_text(size = 8, colour = "black"),
                              axis.text.x = element_text(size = 8, colour = "black"),
                              panel.border = element_blank(),
                              legend.key = element_rect(colour = "white",fill = "white"),
                              legend.text = element_text(size=10,color="black"),
                              legend.title = element_text(size = 11, face = "bold", colour = "#535353"),
                              strip.text.x = element_text(size = 11, face = "bold", colour = "#535353"),
                              strip.background = element_rect(colour = "white", fill = "white"),
                              legend.position="none",
                              plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"))


###########################################################################
#################     Create DF of opinion and votes     ##################
###########################################################################
getwd()

votes <- read.csv(file = "roll_call_votes_final.csv" , stringsAsFactors=FALSE)
opinion<- read.csv(file = "opinion_estimates_uncertainty.csv" , stringsAsFactors =FALSE)
opinion <- subset(opinion , issue!="Partial Birth Abortion (2006)")

#### Create dataframe with opinion and votes
df <- merge(opinion , votes , by = c("state","issue"))
df <- subset(df , vote!=9)
df$party[df$party%in%c("I","ID")] <- "D"


###########################################################################
###############                   Figure 6                 ################
###########################################################################

ggdata <- df
ggdata$Partisan <-ggdata$OutPartisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]
ggdata$OutPartisan[ggdata$party=="D"] <- ggdata$Rep[ggdata$party=="D"]
ggdata$OutPartisan[ggdata$party=="R"] <- ggdata$Dem[ggdata$party=="R"]
ggdata$PartisanQ1 <- ggdata$PartisanQ5 <- NA
ggdata$PartisanQ1[ggdata$party=="D"] <- ggdata$Q1D[ggdata$party=="D"]
ggdata$PartisanQ5[ggdata$party=="D"] <- ggdata$Q5D[ggdata$party=="D"]
ggdata$PartisanQ1[ggdata$party=="R"] <- ggdata$Q1R[ggdata$party=="R"]
ggdata$PartisanQ5[ggdata$party=="R"] <- ggdata$Q5R[ggdata$party=="R"]

ggdata0 <- ddply(ggdata , .(party,simnum) , function(x) out = data.frame(
  median = mean(x$SW > 0.5 & x$vote == 1 | x$SW < 0.5 & x$vote == 0 ),
  poor = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
  rich = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
  partisan = mean(x$Partisan > 0.5 & x$vote == 1 | x$Partisan < 0.5 & x$vote == 0 ),
  outpartisan = mean(x$OutPartisan > 0.5 & x$vote == 1 | x$OutPartisan < 0.5 & x$vote == 0 ),
  richpartisan = mean(x$PartisanQ5 > 0.5 & x$vote == 1 | x$PartisanQ5 < 0.5 & x$vote == 0 ),
  poorpartisan = mean(x$PartisanQ1 > 0.5 & x$vote == 1 | x$PartisanQ1 < 0.5 & x$vote == 0 ),
  rich.disagree = mean(x$Q5[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] > 0.5 &
                         x$vote[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] == 1 |
                         x$Q5[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] < 0.5 &
                         x$vote[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] == 0 ),
  partisan.disagree = mean(x$Partisan[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ] > 0.5 &
                             x$vote[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ]  == 1 |
                             x$Partisan[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ] < 0.5 &
                             x$vote[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ]  == 0 )))

ggdata0 <- rbind.data.frame(ggdata0 ,
  ddply(ggdata , .(simnum)  , function(x) out = data.frame(
    party = "All",
    median = mean(x$SW > 0.5 & x$vote == 1 | x$SW < 0.5 & x$vote == 0 ),
    poor = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
    rich = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
    partisan = mean(x$Partisan > 0.5 & x$vote == 1 | x$Partisan < 0.5 & x$vote == 0 ),
    outpartisan = mean(x$OutPartisan > 0.5 & x$vote == 1 | x$OutPartisan < 0.5 & x$vote == 0 ),
    richpartisan = mean(x$PartisanQ5 > 0.5 & x$vote == 1 | x$PartisanQ5 < 0.5 & x$vote == 0 ),
    poorpartisan = mean(x$PartisanQ1 > 0.5 & x$vote == 1 | x$PartisanQ1 < 0.5 & x$vote == 0 ),
    rich.disagree = mean(x$Q5[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] > 0.5 &
                           x$vote[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] == 1 |
                           x$Q5[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] < 0.5 &
                           x$vote[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] == 0 ),
    partisan.disagree = mean(x$Partisan[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ] > 0.5 &
                               x$vote[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ]  == 1 |
                               x$Partisan[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ] < 0.5 &
                               x$vote[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ]  == 0 ))
))

ggdata0 <- ggdata0[,1:9]

ddply(ggdata0 , .(party) , function(x) apply(x[,3:9] , 2 , range))
ddply(ggdata0 , .(party) , function(x) apply(x[,3:9] , 2 , function(y) out = quantile(y , probs = c(0.005,0.5,0.995))))

gggraph <- melt(ddply(ggdata0 , .(party) , function(x) apply(x[,3:9] , 2 , function(y) out = quantile(y , probs = c(0.005,0.5,0.995)))))
gggraph$Y <- rep(c("CIlow","Median","CIhigh") , 21)
levels(gggraph$variable) <- c("Statewide","Poor","Rich","Co-partisan","Out-partisan","Rich\nCo-partisan","Poor\nCo-partisan")
gggraph$party <- factor(gggraph$party)
levels(gggraph$party) <- c("All","Dems","Reps")

gggraph <- cast(gggraph , party + variable + value ~ Y) %>%
  ddply(. , .(party,variable) , function(x) out = data.frame(
    CIlow=na.omit(x$CIlow),
    Median=na.omit(x$Median),
    CIhigh=na.omit(x$CIhigh)
  ))


pdf("C:/Users/adamz/Dropbox/RSF Ineqaulity Project/Replication File/Figures for Paper/Figure_6_congruence_CI.pdf",height=6,width=12,onefile=FALSE)
  ggplot(gggraph  , aes(x=variable, group = party, y=Median,ymin=CIlow , ymax=CIhigh)) +
  geom_point(position=position_dodge(width = 0.25) ,aes(colour = party, shape = party), size = 2) +
  geom_errorbar(position=position_dodge(width = 0.25) ,aes(colour = party, width=0)) +
  labs(x="\nConstituents\n") +
  scale_y_continuous(name="\nCongruence with ...\n", limits=c(-.02, 1.0), breaks = c(0,0.5,1), labels = c("0%","50%","100%")) +
  ggtheme_personalized +
  theme(plot.margin = unit(c(0.,0.5,0.,0.), "cm"),
        legend.title=element_blank(),
        legend.position = "top",
        panel.grid.major.y = element_line(colour = "black",size=0.5),
        panel.grid.minor.y = element_line(colour = "black",size=0.25),
        legend.text = element_text(size = 14),
        axis.text.x=element_text(size=14,color="black",vjust=0.5, margin=margin(-15,0,0,0)),
        axis.text.y=element_text(size=14,color="black",hjust=0.5, margin=margin(-15,0,0,0)),
        axis.title.x=element_text(size=16,color="black"),
        axis.title.y=element_text(size=16,color="black")) +
  guides(colour = guide_legend(nrow = 1) , position = "bottom") +
  scale_colour_manual(values = c("black","#000069","#b10f2f")) +
  theme(legend.key = element_rect(colour = "white",fill = "white"))
  dev.off()



###########################################################################
###########        TAKING SIDES SUMMARY (Figures 16 and A5)      ##########
###########################################################################
taking_sides_fun <- function(DATA , G1 , G2 , PAR , LAB1 , LAB2){
  data.temp <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] < 0.5  & party==PAR|
                        DATA[,G1] < 0.5 & DATA[,G2] > 0.5  & party==PAR)
  out <- data.frame(N = nrow(data.temp),
                    Value = sum(data.temp[,G1] > 0.5 & data.temp[,'vote']==1 |
                                  data.temp[,G1] < 0.5 & data.temp[,'vote']==0 ),
                    Label1 = LAB1,
                    Label2 = LAB2,
                    Group = ifelse(PAR=="D","Democrats","Republicans"))
  return(out)
}
taking_sides_fun_sims <- function(DATA , G1 , G2 , PAR , LAB1 , LAB2){
  ddply(DATA , .(simnum) , function(x) taking_sides_fun(x , G1 , G2 , PAR , LAB1 , LAB2))
}
taking_sides_fun2 <- function(DATA , G1 , G2 , G3 , PAR , LAB1 , LAB2){
  data.temp <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] > 0.5 & DATA[,G3] < 0.5  & abs(DATA[,G1] - DATA[,G3]) >= 0.1   & party==PAR|
                        DATA[,G1] < 0.5 & DATA[,G2] < 0.5 & DATA[,G3] > 0.5  & abs(DATA[,G1] - DATA[,G3]) >= 0.1   & party==PAR)
  out <- data.frame(N = nrow(data.temp),
                    Value = sum(data.temp[,G1] > 0.5 & data.temp[,'vote']==1 |
                                  data.temp[,G1] < 0.5 & data.temp[,'vote']==0 ),
                    Label1 = LAB1,
                    Label2 = LAB2,
                    Group = ifelse(PAR=="D","Democrats","Republicans"))
  return(out)
}
taking_sides_fun2_sims <- function(DATA , G1 , G2 , G3, PAR , LAB1 , LAB2){
  ddply(DATA , .(simnum) , function(x) taking_sides_fun2(x , G1 , G2 , G3, PAR , LAB1 , LAB2))
}

ggdata0 <- ddply(df , .(issue) , function(x) out = data.frame(
  medianRep = mean(x$vote[x$party=="R"]),
  medianDem = mean(x$vote[x$party=="D"])))
ggdata1 <- join(df , ggdata0)

ggdata1$OutPartisan <-ggdata1$Partisan <- ggdata1$PartisanElite <- NA
ggdata1$Partisan[ggdata1$party=="D"] <- ggdata1$Dem[ggdata1$party=="D"]
ggdata1$Partisan[ggdata1$party=="R"] <- ggdata1$Rep[ggdata1$party=="R"]
ggdata1$OutPartisan[ggdata1$party=="D"] <- ggdata1$Rep[ggdata1$party=="D"]
ggdata1$OutPartisan[ggdata1$party=="R"] <- ggdata1$Dem[ggdata1$party=="R"]
ggdata1$PartisanElite[ggdata1$party=="D"] <- ggdata1$medianDem[ggdata1$party=="D"]
ggdata1$PartisanElite[ggdata1$party=="R"] <- ggdata1$medianRep[ggdata1$party=="R"]

ggdata1$PartisanQ1 <- ggdata1$PartisanQ5 <- ggdata1$PartisanQ3 <- NA
ggdata1$PartisanQ1[ggdata1$party=="D"] <- ggdata1$Q1D[ggdata1$party=="D"]
ggdata1$PartisanQ5[ggdata1$party=="D"] <- ggdata1$Q5D[ggdata1$party=="D"]
ggdata1$PartisanQ1[ggdata1$party=="R"] <- ggdata1$Q1R[ggdata1$party=="R"]
ggdata1$PartisanQ5[ggdata1$party=="R"] <- ggdata1$Q5R[ggdata1$party=="R"]
ggdata1$PartisanQ3[ggdata1$party=="D"] <- ggdata1$Q3D[ggdata1$party=="D"]
ggdata1$PartisanQ3[ggdata1$party=="R"] <- ggdata1$Q3R[ggdata1$party=="R"]

ggdata <- taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "D" , "Rich" , "Poor")
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "D" , "Rich" , "Middle"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "D" , "Middle Class" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "D" , "Rich" , "Statewide"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1" , "D" , "Rich Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ3" , "D" , "Rich Partisans" , "Middle Class Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ3" , "PartisanQ1" , "D" , "Middle Class Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "Partisan" , "D" , "Rich Partisans" , "Partisans"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "OutPartisan" , "Partisan" , "D" , "Opposing Partisans" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "SW" , "Partisan" , "D" , "Statewide" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanElite" , "Partisan" , "D" , "Partisan Senate Peers" , "Partisans"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan" , "D" , "Rich" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan" , "D" , "Rich (National)" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Partisan" , "D" , "Middle Class" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3N" , "Partisan" , "D" , "Middle Class (National)" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite" , "D" , "Rich" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "PartisanElite" , "D" , "Middle Class" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "D" , "Rich Partisans" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "D" , "Rich and Partisans" , "Partisan Senate Peers"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "R" , "Rich" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "R" , "Rich" , "Middle"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "R" , "Middle Class" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "R" , "Rich" , "Statewide"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1" , "R" , "Rich Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ3" , "R" , "Rich Partisans" , "Middle Class Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ3" , "PartisanQ1" , "R" , "Middle Class Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "Partisan" , "R" , "Rich Partisans" , "Partisans"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "OutPartisan" , "Partisan" , "R" , "Opposing Partisans" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "SW" , "Partisan" , "R" , "Statewide" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanElite" , "Partisan" , "R" , "Partisan Senate Peers" , "Partisans"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan" , "R" , "Rich" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan" , "R" , "Rich (National)" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Partisan" , "R" , "Middle Class" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3N" , "Partisan" , "R" , "Middle Class (National)" , "Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite" , "R" , "Rich" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "PartisanElite" , "R" , "Middle Class" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "R" , "Rich Partisans" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "R" , "Rich and Partisans" , "Partisan Senate Peers"))

  ggdata$X <- c(rep(c(29:26, 20:17, 24:22, 15:12 , 10:7),each=500),
                rep(c(29:26, 20:17, 24:22, 15:12 , 10:7),each=500))## 6 is cutoff

  ggdata$Label1 <- paste0("  ",ggdata$Label1)
  ggdata$Label2 <- paste0(ggdata$Label2,"  ")
  ggdata$Value <- ggdata$Value/ggdata$N

  gggdata <- ddply(ggdata , .(Group , X , Label1 , Label2) , function(x){
    out = data.frame(
      N = quantile(x$N , probs = 0.5),
      Median = quantile(x$Value , probs = 0.5),
      CIlow = quantile(x$Value , probs = 0.005),
      CIhigh = quantile(x$Value , probs = 0.995)
    )})
  gggdata <-   gggdata[order(gggdata$Group,-gggdata$X),]

  pdf("C:/Users/adamz/Dropbox/RSF Ineqaulity Project/Replication File/Figures for Paper/Figure_16_master_taking_sides2B_uncertainty.pdf",height=9,width=6,onefile=FALSE)
  ggplot(gggdata, aes(y=Median*100, x=X, group = Group,colour=Group, shape = Group)) +
    geom_segment(aes(y = CIlow*100 , yend = CIhigh*100 , xend = X) , size = 0.5 , alpha = 0.5)  +
    geom_point(stat='identity', aes(size = sqrt(N)))  +
    geom_segment(aes(y = 50,x = 25.8,yend = 50,xend = 29.2),color = "black",linetype=3) +
    geom_segment(aes(y = 0,x = 25.8,yend = 0,xend = 29.2),color = "black",linetype=1) +
    geom_segment(aes(y = 100,x = 25.8,yend = 100,xend = 29.2),color = "black",linetype=1) +
    geom_segment(aes(y = 50,x = 21.8,yend = 50,xend = 24.2),color = "black",linetype=3) +
    geom_segment(aes(y = 0,x = 21.8,yend = 0,xend = 24.2),color = "black",linetype=1) +
    geom_segment(aes(y = 100,x = 21.8,yend = 100,xend = 24.2),color = "black",linetype=1) +
    geom_segment(aes(y = 50,x = 16.8,yend = 50,xend = 20.2),color = "black",linetype=3) +
    geom_segment(aes(y = 0,x = 16.8,yend = 0,xend = 20.2),color = "black",linetype=1) +
    geom_segment(aes(y = 100,x = 16.8,yend = 100,xend = 20.2),color = "black",linetype=1) +
    geom_segment(aes(y = 50,x = 11.8,yend = 50,xend = 15.2),color = "black",linetype=3) +
    geom_segment(aes(y = 0,x = 11.8,yend = 0,xend = 15.2),color = "black",linetype=1) +
    geom_segment(aes(y = 100,x = 11.8,yend = 100,xend = 15.2),color = "black",linetype=1) +
    geom_segment(aes(y = 50,x = 6.8,yend = 50,xend = 10.2),color = "black",linetype=3) +
    geom_segment(aes(y = 0,x = 6.8,yend = 0,xend = 10.2),color = "black",linetype=1) +
    geom_segment(aes(y = 100,x = 6.8,yend = 100,xend = 10.2),color = "black",linetype=1) +
    ylim(-40, 140) +
    scale_x_continuous(minor_breaks=waiver(), labels = NULL, breaks=round(gggdata$X), limits=c(6.8 , 30)) +
    coord_flip()+
    ggtheme_personalized +
    theme(legend.position = c(0.5, 0.02),
          legend.direction = "horizontal",
          legend.box = "horizontal",
          panel.grid.major.y=element_blank(),
          legend.title=element_blank(),
          legend.key = element_rect(colour = "white",fill = "white"),
          legend.text = element_text(size=10,color="black"),
          axis.title.y = element_blank(),
          axis.text.y = element_text(size=8,color="black"),
          plot.margin = unit(c(0.,0.,0.,0.), "cm"),
          axis.title.x = element_blank(),
          axis.text.x = element_blank()) +
    guides(size = "none", alpha="none" , density="none" , fill = "none") +
    geom_text(data = data.frame(X = round(gggdata$X[1:19]), Label=gggdata$Label1[1:19],Median = rep(1,19),Group=rep("Republicans",19)) , aes(label = Label), size = 3, colour = "black",hjust=0)+
    geom_text(data = data.frame(X = round(gggdata$X[1:19]), Label=gggdata$Label2[1:19],Median = rep(0,19),Group=rep("Republicans",19)) , aes(label = Label), size = 3, colour = "black",hjust=1)+
    annotate("text" , x = 29.4 , y= 50 , label = "50%" , size = 3) +
    annotate("text" , x = 29.4 , y= 100 , label = "100%" , size = 3) +
    annotate("text" , x = 30 , y= 50 , label = "Battles of the Purse") +
    annotate("text" , x = 25 , y= 50 , label = "Battles of Partisans") +
    annotate("text" , x = 21 , y= 50 , label = "Battles of the Purse within Parties") +
    annotate("text" , x = 16 , y= 50 , label = "Party vs. the Purse") +
    annotate("text" , x = 11 , y= 50 , label = "Party Elites vs. the Purse") +
    scale_colour_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
    scale_fill_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
    scale_shape_manual(breaks = c("Democrats","Republicans"), values = c(21,22), labels = c("Democratic Senators","Republican Senators"))
  dev.off()






  ###########################################################################
  ###############                  Figure A5                 ################
  ###########################################################################

  taking_sides_fun <- function(DATA , G1 , G2 , PAR , LAB1 , LAB2){
    data.temp <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] < 0.5  & abs(DATA[,G1] - DATA[,G2]) >= 0.1 & party==PAR|
                          DATA[,G1] < 0.5 & DATA[,G2] > 0.5  & abs(DATA[,G1] - DATA[,G2]) >= 0.1 & party==PAR)
    out <- data.frame(N = nrow(data.temp),
                      Value = sum(data.temp[,G1] > 0.5 & data.temp[,'vote']==1 |
                                    data.temp[,G1] < 0.5 & data.temp[,'vote']==0 ),
                      Label1 = LAB1,
                      Label2 = LAB2,
                      Group = ifelse(PAR=="D","Democrats","Republicans"))
    return(out)
  }

  taking_sides_fun2 <- function(DATA , G1 , G2 , G3 , PAR , LAB1 , LAB2){
    data.temp <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] > 0.5 & DATA[,G3] < 0.5  & abs(DATA[,G1] - DATA[,G3]) >= 0.1   & abs(DATA[,G2] - DATA[,G3]) >= 0.1 & party==PAR|
                          DATA[,G1] < 0.5 & DATA[,G2] < 0.5 & DATA[,G3] > 0.5  & abs(DATA[,G1] - DATA[,G3]) >= 0.1   & abs(DATA[,G2] - DATA[,G3]) >= 0.1 & party==PAR)
    out <- data.frame(N = nrow(data.temp),
                      Value = sum(data.temp[,G1] > 0.5 & data.temp[,'vote']==1 |
                                    data.temp[,G1] < 0.5 & data.temp[,'vote']==0 ),
                      Label1 = LAB1,
                      Label2 = LAB2,
                      Group = ifelse(PAR=="D","Democrats","Republicans"))
    return(out)
  }


  ggdata <- taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "D" , "Rich" , "Poor")
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "D" , "Rich" , "Statewide"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan" , "D" , "Rich" , "Partisan"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan" , "D" , "Rich (National)" , "Partisan"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1" , "D" , "Rich Partisans" , "Poor Partisans"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite" , "D" , "Rich" , "Partisan Senate Peers"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "D" , "Rich" , "Middle"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "D" , "Middle" , "Poor"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Rep" , "Dem" , "D" , "Rep" , "Dem"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "SW" , "D" , "Partisan" , "Statewide"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "PartisanElite" , "D" , "Partisan" , "Partisan Senate Peers"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "D" , "Rich Partisans" , "Partisan Senate Peers"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "D" , "Rich and Partisan" , "Partisan Senate Peers"))

  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "R" , "Rich" , "Poor"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "R" , "Rich" , "Statewide"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan" , "R" , "Rich" , "Partisan"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan" , "R" , "Rich (National)" , "Partisan"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1" , "R" , "Rich Partisans" , "Poor Partisans"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite" , "R" , "Rich" , "Partisan Senate Peers"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "R" , "Rich" , "Middle"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "R" , "Middle" , "Poor"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Rep" , "Dem" , "R" , "Rep" , "Dem"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "SW" , "R" , "Partisan" , "Statewide"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "PartisanElite" , "R" , "Partisan" , "Partisan Senate Peers"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "R" , "Rich Partisans" , "Partisan Senate Peers"))
  ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "R" , "Rich and Partisan" , "Partisan Senate Peers"))

  ggdata$X <- c(rep(c(19,18,17,16, 15, 14, 13,12,9,8, 7,6,5),each=500),
                rep(c(19,18,17,16, 15, 14, 13,12,9,8, 7,6,5),each=500))

  ggdata$Label1 <- paste0("  ",ggdata$Label1)
  ggdata$Label2 <- paste0(ggdata$Label2,"  ")
  ggdata$Value <- ggdata$Value/ggdata$N

  gggdata <- ddply(ggdata , .(Group , X , Label1 , Label2) , function(x){
    out = data.frame(
      N = quantile(x$N , probs = 0.5),
      Median = quantile(x$Value , probs = 0.5),
      CIlow = quantile(x$Value , probs = 0.005),
      CIhigh = quantile(x$Value , probs = 0.995)
    )})
  gggdata <-   gggdata[order(gggdata$Group,-gggdata$X),]

pdf(paste0(path1 , "/Figures for Paper/Figure_A5a_master_taking_sides_10pp_uncertainty.pdf"),height=6,width=6,onefile=FALSE)
ggplot(gggdata, aes(y=Median*100, x=X, group = Group,colour=Group, shape = Group)) +
  geom_segment(aes(y = CIlow*100 , yend = CIhigh*100 , xend = X) , size = 0.5 , alpha = 0.5)  +
  geom_point(stat='identity', aes(size = sqrt(N*3 / pi)))  +
  geom_segment(aes(y = 50,x = 10.8,yend = 50,xend = 19.2),color = "black",linetype=3) +
  geom_segment(aes(y = 0,x = 10.8,yend = 0,xend = 19.2),color = "black",linetype=1) +
  geom_segment(aes(y = 100,x = 10.8,yend = 100,xend = 19.2),color = "black",linetype=1) +
  geom_segment(aes(y = 50,x = 5,yend = 50,xend = 9.2),color = "black",linetype=3) +
  geom_segment(aes(y = 0,x = 5,yend = 0,xend = 9.2),color = "black",linetype=1) +
  geom_segment(aes(y = 100,x = 5,yend = 100,xend = 9.2),color = "black",linetype=1) +
  ylim(-40, 140) +
  scale_x_continuous(minor_breaks=waiver(), labels = NULL, breaks=round(gggdata$X), limits=c(4.8 , 20)) +
  coord_flip()+
  ggtheme_personalized +
  theme(legend.position="bottom",
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.key = element_rect(colour = "white",fill = "white"),
        legend.text = element_text(size=10,color="black"),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size=8,color="black"),
        plot.margin = unit(c(1.,0.,1.,0.), "cm"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank()) +
  guides(size = "none", alpha="none" , density="none" , fill = "none") +
  geom_text(data = data.frame(X = round(gggdata$X[1:13]), Label=gggdata$Label1[1:13],Median = rep(1,13),Group=rep("Republicans",13)) , aes(label = Label), size = 3, colour = "black",hjust=0)+
  geom_text(data = data.frame(X = round(gggdata$X[1:13]), Label=gggdata$Label2[1:13],Median = rep(0,13),Group=rep("Republicans",13)) , aes(label = Label), size = 3, colour = "black",hjust=1)+
  annotate("text" , x = 19.4 , y= 50 , label = "50%" , size = 3) +
  annotate("text" , x = 19.4 , y= 100 , label = "100%" , size = 3) +
  annotate("text" , x = 20 , y= 50 , label = "Whom Do Rich Voters 'Win' Against?") +
  annotate("text" , x = 10 , y= 50 , label = "Whom Do Partisan Voters 'Win' Against?") +
  scale_colour_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
  scale_fill_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
  scale_shape_manual(breaks = c("Democrats","Republicans"), values = c(21,22), labels = c("Democratic Senators","Republican Senators"))
dev.off()




taking_sides_fun <- function(DATA , G1 , G2 , PAR , LAB1 , LAB2){
  data.temp <- subset(DATA , abs(0.5 - DATA[,G1]) > 0.05 &abs(0.5 - DATA[,G2]) > 0.05 &
                        DATA[,G1] > 0.5 & DATA[,G2] < 0.5  & abs(DATA[,G1] - DATA[,G2]) >= 0.1 & party==PAR|
                        abs(0.5 - DATA[,G1]) > 0.05 &abs(0.5 - DATA[,G2]) > 0.05 &
                        DATA[,G1] < 0.5 & DATA[,G2] > 0.5  & abs(DATA[,G1] - DATA[,G2]) >= 0.1 & party==PAR)
  out <- data.frame(N = nrow(data.temp),
                    Value = sum(data.temp[,G1] > 0.5 & data.temp[,'vote']==1 |
                                  data.temp[,G1] < 0.5 & data.temp[,'vote']==0 ),
                    Label1 = LAB1,
                    Label2 = LAB2,
                    Group = ifelse(PAR=="D","Democrats","Republicans"))
  return(out)
}

taking_sides_fun2 <- function(DATA , G1 , G2 , G3 , PAR , LAB1 , LAB2){
  data.temp <- subset(DATA , abs(0.5 - DATA[,G1]) > 0.05 &abs(0.5 - DATA[,G2]) > 0.05 & abs(0.5 - DATA[,G3]) > 0.05 &
                        DATA[,G1] > 0.5 & DATA[,G2] > 0.5 & DATA[,G3] < 0.5  & abs(DATA[,G1] - DATA[,G3]) >= 0.1   & abs(DATA[,G2] - DATA[,G3]) >= 0.1 & party==PAR|
                        abs(0.5 - DATA[,G1]) > 0.05 &abs(0.5 - DATA[,G2]) > 0.05 & abs(0.5 - DATA[,G3]) > 0.05 &
                        DATA[,G1] < 0.5 & DATA[,G2] < 0.5 & DATA[,G3] > 0.5  & abs(DATA[,G1] - DATA[,G3]) >= 0.1   & abs(DATA[,G2] - DATA[,G3]) >= 0.1 & party==PAR)
  out <- data.frame(N = nrow(data.temp),
                    Value = sum(data.temp[,G1] > 0.5 & data.temp[,'vote']==1 |
                                  data.temp[,G1] < 0.5 & data.temp[,'vote']==0 ),
                    Label1 = LAB1,
                    Label2 = LAB2,
                    Group = ifelse(PAR=="D","Democrats","Republicans"))
  return(out)
}

ggdata <- taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "D" , "Rich" , "Poor")
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "D" , "Rich" , "Statewide"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan" , "D" , "Rich" , "Partisan"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan" , "D" , "Rich (National)" , "Partisan"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1" , "D" , "Rich Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite" , "D" , "Rich" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "D" , "Rich" , "Middle"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "D" , "Middle" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Rep" , "Dem" , "D" , "Rep" , "Dem"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "SW" , "D" , "Partisan" , "Statewide"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "PartisanElite" , "D" , "Partisan" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "D" , "Rich Partisans" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "D" , "Rich and Partisan" , "Partisan Senate Peers"))

ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "R" , "Rich" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "R" , "Rich" , "Statewide"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan" , "R" , "Rich" , "Partisan"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan" , "R" , "Rich (National)" , "Partisan"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1" , "R" , "Rich Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite" , "R" , "Rich" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "R" , "Rich" , "Middle"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "R" , "Middle" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Rep" , "Dem" , "R" , "Rep" , "Dem"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "SW" , "R" , "Partisan" , "Statewide"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "PartisanElite" , "R" , "Partisan" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "R" , "Rich Partisans" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "R" , "Rich and Partisan" , "Partisan Senate Peers"))

ggdata$X <- c(rep(c(19,18,17,16, 15, 14, 13,12,9,8, 7,6,5),each=500),
              rep(c(19,18,17,16, 15, 14, 13,12,9,8, 7,6,5),each=500))

ggdata$Label1 <- paste0("  ",ggdata$Label1)
ggdata$Label2 <- paste0(ggdata$Label2,"  ")
ggdata$Value <- ggdata$Value/ggdata$N

gggdata <- ddply(ggdata , .(Group , X , Label1 , Label2) , function(x){
  out = data.frame(
    N = quantile(x$N , probs = 0.5),
    Median = quantile(x$Value , probs = 0.5),
    CIlow = quantile(x$Value , probs = 0.005),
    CIhigh = quantile(x$Value , probs = 0.995)
  )})
gggdata <-   gggdata[order(gggdata$Group,-gggdata$X),]

pdf(paste0(path1 , "/Figures for Paper/Figure_A5b_master_taking_sides_10pp_extreme_uncertainty.pdf"),height=6,width=6,onefile=FALSE)
ggplot(gggdata, aes(y=Median*100, x=X, group = Group,colour=Group, shape = Group)) +
  geom_segment(aes(y = CIlow*100 , yend = CIhigh*100 , xend = X) , size = 0.5 , alpha = 0.5)  +
  geom_point(stat='identity', aes(size = sqrt(N*3 / pi)))  +
  geom_segment(aes(y = 50,x = 10.8,yend = 50,xend = 19.2),color = "black",linetype=3) +
  geom_segment(aes(y = 0,x = 10.8,yend = 0,xend = 19.2),color = "black",linetype=1) +
  geom_segment(aes(y = 100,x = 10.8,yend = 100,xend = 19.2),color = "black",linetype=1) +
  geom_segment(aes(y = 50,x = 5,yend = 50,xend = 9.2),color = "black",linetype=3) +
  geom_segment(aes(y = 0,x = 5,yend = 0,xend = 9.2),color = "black",linetype=1) +
  geom_segment(aes(y = 100,x = 5,yend = 100,xend = 9.2),color = "black",linetype=1) +
  ylim(-40, 140) +
  scale_x_continuous(minor_breaks=waiver(), labels = NULL, breaks=round(gggdata$X), limits=c(4.8 , 20)) +
  coord_flip()+
  ggtheme_personalized +
  theme(legend.position="bottom",
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.key = element_rect(colour = "white",fill = "white"),
        legend.text = element_text(size=10,color="black"),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size=8,color="black"),
        plot.margin = unit(c(1.,0.,1.,0.), "cm"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank()) +
  guides(size = "none", alpha="none" , density="none" , fill = "none") +
  geom_text(data = data.frame(X = round(gggdata$X[1:13]), Label=gggdata$Label1[1:13],Median = rep(1,13),Group=rep("Republicans",13)) , aes(label = Label), size = 3, colour = "black",hjust=0)+
  geom_text(data = data.frame(X = round(gggdata$X[1:13]), Label=gggdata$Label2[1:13],Median = rep(0,13),Group=rep("Republicans",13)) , aes(label = Label), size = 3, colour = "black",hjust=1)+
  annotate("text" , x = 19.4 , y= 50 , label = "50%" , size = 3) +
  annotate("text" , x = 19.4 , y= 100 , label = "100%" , size = 3) +
  annotate("text" , x = 20 , y= 50 , label = "Whom Do Rich Voters 'Win' Against?") +
  annotate("text" , x = 10 , y= 50 , label = "Whom Do Partisan Voters 'Win' Against?") +
  scale_colour_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
  scale_fill_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
  scale_shape_manual(breaks = c("Democrats","Republicans"), values = c(21,22), labels = c("Democratic Senators","Republican Senators"))
dev.off()



taking_sides_fun <- function(DATA , G1 , G2 , LAB1 , LAB2){
  data.tempD <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] < 0.5  & party=="D"|
                         DATA[,G1] < 0.5 & DATA[,G2] > 0.5  & party=="D")
  data.tempR <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] < 0.5  & party=="R"|
                         DATA[,G1] < 0.5 & DATA[,G2] > 0.5  & party=="R")
  data.tempD$issue_state <- paste0(data.tempD$issue,data.tempD$state)
  data.tempR$issue_state <- paste0(data.tempR$issue,data.tempR$state)
  issue_state <- intersect(data.tempD$issue_state , data.tempR$issue_state)
  data.tempD <- subset(data.tempD , issue_state%in%issue_state)
  data.tempR <- subset(data.tempR , issue_state%in%issue_state)

  outD <- data.frame(N = nrow(data.tempD),
                     Value = sum(data.tempD[,G1] > 0.5 & data.tempD[,'vote']==1 |
                                   data.tempD[,G1] < 0.5 & data.tempD[,'vote']==0 ),
                     Label1 = LAB1,
                     Label2 = LAB2,
                     Group = "Democrats")
  outR <- data.frame(N = nrow(data.tempR),
                     Value = sum(data.tempR[,G1] > 0.5 & data.tempR[,'vote']==1 |
                                   data.tempR[,G1] < 0.5 & data.tempR[,'vote']==0 ),
                     Label1 = LAB1,
                     Label2 = LAB2,
                     Group = "Republicans")
  return(rbind.data.frame(outD,outR))
}

taking_sides_fun2 <- function(DATA , G1 , G2 , G3 , LAB1 , LAB2){
  data.tempD <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] > 0.5 & DATA[,G3] < 0.5 & party=="D"|
                         DATA[,G1] < 0.5 & DATA[,G2] < 0.5 & DATA[,G3] > 0.5 & party=="D")
  data.tempR <- subset(DATA , DATA[,G1] > 0.5 & DATA[,G2] > 0.5 & DATA[,G3] < 0.5 & party=="R"|
                         DATA[,G1] < 0.5 & DATA[,G2] < 0.5 & DATA[,G3] > 0.5 & party=="R")
  data.tempD$issue_state <- paste0(data.tempD$issue,data.tempD$state)
  data.tempR$issue_state <- paste0(data.tempR$issue,data.tempR$state)
  issue_state <- intersect(data.tempD$issue_state , data.tempR$issue_state)
  data.tempD <- subset(data.tempD , issue_state%in%issue_state)
  data.tempR <- subset(data.tempR , issue_state%in%issue_state)

  outD <- data.frame(N = nrow(data.tempD),
                     Value = sum(data.tempD[,G1] > 0.5 & data.tempD[,'vote']==1 |
                                   data.tempD[,G1] < 0.5 & data.tempD[,'vote']==0 ),
                     Label1 = LAB1,
                     Label2 = LAB2,
                     Group = "Democrats")
  outR <- data.frame(N = nrow(data.tempR),
                     Value = sum(data.tempR[,G1] > 0.5 & data.tempR[,'vote']==1 |
                                   data.tempR[,G1] < 0.5 & data.tempR[,'vote']==0 ),
                     Label1 = LAB1,
                     Label2 = LAB2,
                     Group = "Republicans")
  return(rbind.data.frame(outD,outR))
}

taking_sides_fun_sims <- function(DATA , G1 , G2 , LAB1 , LAB2){
  ddply(DATA , .(simnum) , function(x) taking_sides_fun(x , G1 , G2 , LAB1 , LAB2))
}
taking_sides_fun2_sims <- function(DATA , G1 , G2 , G3, LAB1 , LAB2){
  ddply(DATA , .(simnum) , function(x) taking_sides_fun2(x , G1 , G2 , G3 , LAB1 , LAB2))
}


ggdata <- taking_sides_fun_sims(ggdata1 , "Q5" , "Q1" , "Rich" , "Poor")
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "SW" , "Rich" , "Statewide"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Partisan", "Rich" , "Partisan"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5N" , "Partisan"  , "Rich (National)" , "Partisan"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanQ1"  , "Rich Partisans" , "Poor Partisans"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "PartisanElite"  , "Rich" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q5" , "Q3" , "Rich" , "Middle"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Q3" , "Q1" , "Middle" , "Poor"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Rep" , "Dem" ,"Rep" , "Dem"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "SW" ,"Partisan" , "Statewide"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "Partisan" , "PartisanElite" , "Partisan" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun_sims(ggdata1 , "PartisanQ5" , "PartisanElite" , "Rich Partisans" , "Partisan Senate Peers"))
ggdata <- rbind.data.frame(ggdata , taking_sides_fun2_sims(ggdata1 , "Q5" , "Partisan" , "PartisanElite" , "Rich and Partisan" , "Partisan Senate Peers"))

ggdatat1 <-   ggdata[order(ggdata$Group),]
ggdatat1$X <- c(rep(c(19,18,17,16, 15, 14, 13,12,9,8, 7,6,5),each=500),
              rep(c(19,18,17,16, 15, 14, 13,12,9,8, 7,6,5),each=500))

ggdatat1$Label1 <- paste0("  ",ggdatat1$Label1)
ggdatat1$Label2 <- paste0(ggdatat1$Label2,"  ")
ggdatat1$Value <- ggdatat1$Value/ggdatat1$N

gggdata <- ddply(ggdatat1 , .(Group , X , Label1 , Label2) , function(x){
  out = data.frame(
    N = quantile(x$N , probs = 0.5),
    Median = quantile(x$Value , probs = 0.5),
    CIlow = quantile(x$Value , probs = 0.005),
    CIhigh = quantile(x$Value , probs = 0.995)
  )})
gggdata <-   gggdata[order(gggdata$Group , -gggdata$X),]

pdf(paste0(path1 , "/Figures for Paper/Figure_A5c_master_taking_sides_delegations_uncertainty.pdf"),height=6,width=6,onefile=FALSE)
ggplot(gggdata, aes(y=Median*100, x=X, group = Group,colour=Group, shape = Group)) +
  geom_segment(aes(y = CIlow*100 , yend = CIhigh*100 , xend = X) , size = 0.5 , alpha = 0.5)  +
  geom_point(stat='identity', aes(size = sqrt(N*3 / pi)))  +
  geom_segment(aes(y = 50,x = 10.8,yend = 50,xend = 19.2),color = "black",linetype=3) +
  geom_segment(aes(y = 0,x = 10.8,yend = 0,xend = 19.2),color = "black",linetype=1) +
  geom_segment(aes(y = 100,x = 10.8,yend = 100,xend = 19.2),color = "black",linetype=1) +
  geom_segment(aes(y = 50,x = 5,yend = 50,xend = 9.2),color = "black",linetype=3) +
  geom_segment(aes(y = 0,x = 5,yend = 0,xend = 9.2),color = "black",linetype=1) +
  geom_segment(aes(y = 100,x = 5,yend = 100,xend = 9.2),color = "black",linetype=1) +
  ylim(-40, 140) +
  scale_x_continuous(minor_breaks=waiver(), labels = NULL, breaks=round(gggdata$X), limits=c(4.8 , 20)) +
  coord_flip()+
  ggtheme_personalized +
  theme(legend.position="bottom",
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.key = element_rect(colour = "white",fill = "white"),
        legend.text = element_text(size=10,color="black"),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size=8,color="black"),
        plot.margin = unit(c(1.,0.,1.,0.), "cm"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank()) +
  guides(size = "none", alpha="none" , density="none" , fill = "none") +
  geom_text(data = data.frame(X = round(gggdata$X[1:13]), Label=gggdata$Label1[1:13],Median = rep(1,13),Group=rep("Republicans",13)) , aes(label = Label), size = 3, colour = "black",hjust=0)+
  geom_text(data = data.frame(X = round(gggdata$X[1:13]), Label=gggdata$Label2[1:13],Median = rep(0,13),Group=rep("Republicans",13)) , aes(label = Label), size = 3, colour = "black",hjust=1)+
  annotate("text" , x = 19.4 , y= 50 , label = "50%" , size = 3) +
  annotate("text" , x = 19.4 , y= 100 , label = "100%" , size = 3) +
  annotate("text" , x = 20 , y= 50 , label = "Whom Do Rich Voters 'Win' Against?") +
  annotate("text" , x = 10 , y= 50 , label = "Whom Do Partisan Voters 'Win' Against?") +
  scale_colour_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
  scale_fill_manual(breaks = c("Democrats","Republicans"), values = c("#000069","#b10f2f"), labels = c("Democratic Senators","Republican Senators"))+
  scale_shape_manual(breaks = c("Democrats","Republicans"), values = c(21,22), labels = c("Democratic Senators","Republican Senators"))
dev.off()

